package com.gluck.cases.app.biz.impl.excel;

import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.common.utils.utils.MethodUtil;
import com.gluck.cases.core.modal.dto.TestCaseDTO;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AnalysisExcelHandler
 * @Desc excel 解析handler
 * @Author shiyongzhe
 * @Date 2022/5/28 - 1:46 下午
 **/
public class AnalysisExcelHandler extends AbstractExcelHandler<Workbook> {

    private final static Logger log = LoggerFactory.getLogger(AnalysisExcelHandler.class);
    //一行存为一个Obj,放在List中
    private List<TestCaseDTO> beans = new ArrayList<>();

    @Override
    public void doHandler(Workbook wb, Builder<Workbook> excelBuilder) {
        Sheet sheet = wb.getSheet(excelBuilder.getSheetname());
        int lastRow = sheet.getRow(0).getLastCellNum();
        List<String> headTitles = getHeadTitles(sheet.getRow(0), lastRow);
        for (int i = 1; i < lastRow; i++) {
            try{
                // 获取单元格数据
                getCell(sheet.getRow(i), headTitles, excelBuilder.getInvokeEntity());
            }catch (Exception e){
                log.error(e.getMessage());
                AssertUtil.isTrue(true, BizSerErrorEnum.EXCEL_ERROR);
            }
        }
        excelBuilder.setCaseEntities(beans);
    }

    private void getCell(Row row, List<String> headTitles, Class<?> clazz) throws InstantiationException, IllegalAccessException {
        TestCaseDTO instance = (TestCaseDTO) clazz.newInstance();
        for (int c = 0; c < row.getLastCellNum(); c++) {
            Cell cell = row.getCell(c);
            DataFormatter formatter = new DataFormatter();
            String value = formatter.formatCellValue(cell);
            String headName = headTitles.get(c);
            // 反射对应的实体
            instance = invokeEntity(value, headName, clazz, instance);
        }
        if(null != instance){
            beans.add(instance);
        }
    }

    private TestCaseDTO invokeEntity(String value, String headName, Class<?> clazz,TestCaseDTO instance) {
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                String name = field.getName();
                if (!headName.equalsIgnoreCase(name)) {
                    continue;
                }
                String methodName = MethodUtil.setMethodName(field.getName());
                Method method = clazz.getMethod(methodName, field.getType());
                //注入值
                method.invoke(instance, value);
                return instance;
            }

        } catch (Exception e) {
            log.error(e.getMessage());
            AssertUtil.isTrue(true, BizSerErrorEnum.EXCEL_ERROR);
        }
        return null;
    }

    private List<String> getHeadTitles(Row row, int lastRow) {
        List<String> headList = new ArrayList<>();
        for (int index = 0; index <= lastRow; index++) {
            Cell cell = row.getCell(index);
            if (null == cell) {
                continue;
            }
            String cellValue = cell.getStringCellValue();
            String entityStr = cellValue.split("/")[1].trim();
            headList.add(entityStr);
        }
        return headList;
    }
}
